<?php
/*
 * @Descripttion: 
 * @version: v1
 * @Author: Terry
 * @Date: 2021-11-01 18:36:05
 * @LastEditors: Terry
 * @LastEditTime: 2024-10-26 00:34:41
 */
declare (strict_types = 1);

namespace app\BI\controller\admin\v1;

use think\Request;
use think\facade\Db;
use think\worker\Server;
use Workerman\Lib\Timer;
use libs\LocalCache;

class ConfinedSpace  extends Server{
	protected $socket = 'websocket://0.0.0.0:2350/';
	protected $count = 4;

    protected $HEARTBEAT_TIME = 60;
	// 全局变量，保存当前进程的客户端连接数
	protected $connection_count = 0;
	protected $option = ['count' => 1, 'name' => '有限空间'];
	protected $date;
    protected $uidConnection = [];
	protected $timer = [];
	protected $prjCode;

    public function getData(Request $request){ 
        $data = [
            'name'=>'奥视智能气体监测系统',
            'text'=>'国内领先的建筑业管理系统',
            'logo'=>'https://auxface-oss.oss-cn-shenzhen.aliyuncs.com/asset/logo.png',
            'background'=>[
                'type'=>'map',
                'center'=>[116.397428, 39.90923],
                'url'=>'',
                'stream'=>'',
                'video'=>'',
            ],
            'bi'=>[
                [
                    'name'=>'有限空间',
                    'layout'=>[
                        'left'=>[
                            [
                                'name'=>"notice",
                                'height'=>"30%"
                            ],
                            [
                                'name'=>"trend",
                                'height'=>"70%"
                            ],
                        ],
                        'right'=>[
                            [
                                'name'=>"weather",
                                'height'=>"10%"
                            ],
                            [
                                'name'=>"progressFrom",
                                'height'=>"50%"
                            ],
                            [
                                'name'=>"online",
                                'height'=>"40%"
                            ],
                          
                        ]
                    ],
                    'components'=>["notice","trend","progressFrom","online","weather"],
                ]
            ]
        ];
        return result(SUCCESS,$data);
    }   
    
	public function onConnect($connection){
		$connection->send($this->socketRes(SOCKET_CONNECT_SUCCESS,[],''));
    }
    
	public function onMessage($connection,$data){
        $data = json_decode($data,true);
        dump($data);
        if(!isset($data['prjCode']) ||!isset($data['requestId']) ||!isset($data['components'])){
            $connection->send($this->socketRes(FAIL,[],'数据有误,请重试!'));
            return;
        }
        $prjCode = $data['prjCode'];
        $requestId = $data['requestId'];
        $components = $data['components'];

        $connection->prjCode=$prjCode;
        $connection->requestId = $requestId;
        
        $res = $this->getSocketData($prjCode,$components);
        $connection->send($this->socketRes(SUCCESS,$res,''));
        $this->uidConnection[$requestId] = $connection;
        
        $this->timer[$requestId] = Timer::add(30, function () use ($connection,$components) {
            $res = $this->getSocketData($connection->prjCode,$components);
            $connection->send($this->socketRes(SUCCESS,$res,''));
        });
	}
    public function onClose($connection) {
        $requestId = $connection->requestId ?? ''; // 获取请求 ID
        if(!$requestId) return;
        $uidConnection = $this->uidConnection; // 获取用户连接映射
        $timer = $this->timer; // 获取定时器映射
        
        // 检查并删除定时器
        if (isset($this->timer[$requestId])) {
            Timer::del($this->timer[$requestId]); // 删除定时器
        }
    
        // 从 uidConnection 和 timer 中删除对应的连接信息
        unset($uidConnection[$requestId], $timer[$requestId]);
        
        // 更新当前对象的 uidConnection 和 timer
        $this->uidConnection = $uidConnection;
        $this->timer = $timer;
    
        // 记录连接关闭日志
        error_log("Connection closed: " . $requestId);
    }
    /**
     * @Descripttion: 获取面板数据
     * @Author: Terry
     * @Date: 2021-10-5 18:01:44
     * @param {*}
     */    
    public function getSocketData($prjCode,$components){
        $this->prjCode = $prjCode;
        $data = [];
        foreach ($components as  $value) {
            $module = 'ConfinedSpace';
            $sdkName = "app\\BI\\extend\\components\\{$module}";
            $sdk = new $sdkName($prjCode);
            $data[$value] = $sdk->$value();
        }
        return $data;
    }
    /**
     * @Descripttion: socket格式的返回
     * @Author: Terry
     * @Date: 2021-11-03 19:17:03
     * @param {*} $code
     * @param {*} $data
     * @param {*} $msg
     */
    public function socketRes($code,$data,$msg){
        return json_encode([
            'code'=>$code,
            'data'=>$data,
            'msg'=>$msg,
        ]);
    }
}
